using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows.Forms;

namespace Raider.WinForms.Actions.Standard
{
    /// <summary>
    /// Specifies a list of events to be hidden from the <see cref="PropertyGrid"/>, overriding
    /// the effect of <see cref="BrowsableAttribute"/>.
    /// </summary>
    /// <remarks>
    /// This attribute is applicable only on the <see cref="ControlAction"/> and it's descendants. Specified
    /// events are hidden only in the design mode.
    /// </remarks>
    [AttributeUsage(AttributeTargets.Class)]
    public sealed class HideEventAttribute : Attribute
    {
        private readonly ReadOnlyCollection<string> hiddenEvents;

        /// <summary/>
        internal HideEventAttribute(params string[] eventsToHide)
        {
            hiddenEvents = new ReadOnlyCollection<string>(eventsToHide);
        }

        /// <summary>
        /// Gets a collection of events to be hidden.
        /// </summary>
        public ReadOnlyCollection<string> HiddenEvents
        {
            get { return hiddenEvents; }
        }

        /// <summary/>
        public static HideEventAttribute[] GetAttributes(Type classType)
        {
            return (HideEventAttribute[])GetCustomAttributes(classType,
                typeof(HideEventAttribute), true);
        }

        /// <summary/>
        public static ICollection<string> GetHiddenEvents(Type classType)
        {
            var hiddenEvents = new HashSet<string>(StringComparer.Ordinal);
            foreach (var attribute in GetAttributes(classType))
            {
                hiddenEvents.UnionWith(attribute.HiddenEvents);
            }

            return hiddenEvents;
        }
    }
}